package com.sgb.kjwl.view.ui.adapter.transport;


import android.Manifest;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.common.AddressItem;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.PermissionUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class HandleAddressAdapter extends BaseQuickAdapter<AddressItem, BaseViewHolder> {

    private int select = 0;

    public HandleAddressAdapter(@Nullable List<AddressItem> data) {
        super(R.layout.item_handle_adress, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, AddressItem poi) {
        holder.setVisible(R.id.iv_select, holder.getLayoutPosition() == select);
        holder.setImageResource(R.id.iv_handle, poi.getIcon())
                .setText(R.id.tv_adress, poi.getAddressName());
        if (holder.getLayoutPosition() == 0) {
            float distance = 0l;
            if (PermissionUtils.checkPermissionsGroup(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
                if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""))) {
                    double lat = Double.valueOf(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, ""));
                    double lng = Double.valueOf(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE, ""));
                    LatLng latLngLocation = new LatLng(lat, lng);
                    LatLng current = new LatLng(poi.getLatitude(), poi.getLongitude());
                    distance = AMapUtils.calculateLineDistance(current, latLngLocation) / 1000;
                }
            }
            if (distance > 0) {
                String address = TextUtils.isEmpty(poi.getAddressdetail()) ? "" : poi.getAddressdetail();
                if (distance >= 1) {
                    long distanceL = (long) distance;
                    Spanned str = Html.fromHtml("<font color='#5792FD'>" + String.format("%s", "距您 " + distanceL + "km</font>  " + address));
                    holder.setText(R.id.tv_detail, str);
                } else{
                    Spanned str = Html.fromHtml("<font color='#5792FD'>" + String.format("%s", "距您 " + NumberUtil.trim0(distance, 2) + "km</font>  " + address));
                    holder.setText(R.id.tv_detail, str);
                }
            } else {
                holder.setText(R.id.tv_detail, poi.getAddressdetail());
            }

        } else if (!TextUtils.isEmpty(poi.getAddressdetail())) {
            holder.setText(R.id.tv_detail, poi.getAddressdetail());
        }

    }

    public int getSelect() {
        return select;
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

}
